package cn.xluobo.business.wechat.miniapp.teacher.service;

import cn.xluobo.business.sc.student.domain.req.ReqSearchScStudent;
import cn.xluobo.business.sys.admin.repo.model.SysDept;
import cn.xluobo.business.sys.admin.repo.model.SysUserDept;
import cn.xluobo.business.sys.admin.service.ISysDeptService;
import cn.xluobo.business.sys.admin.service.ISysUserDeptService;
import cn.xluobo.business.sys.staff.domain.req.ReqSearchStaff;
import cn.xluobo.business.sys.staff.domain.resp.RespBusinessStaffDetail;
import cn.xluobo.business.sys.staff.domain.resp.RespStaffInfo;
import cn.xluobo.business.sys.staff.repo.model.SysStaff;
import cn.xluobo.business.sys.staff.service.ISysStaffService;
import cn.xluobo.business.wechat.miniapp.teacher.domain.resp.RespIndexBarStaff;
import cn.xluobo.config.login.LoginUser;
import cn.xluobo.core.api.APIResponse;
import cn.xluobo.core.utils.pinyin.PinYinUtil;
import cn.xluobo.utils.LoginUserUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：KingLong
 * @date ：Created in 2020/11/12 08:39
 */
@Service
public class WechatMiniStaffService {

    @Autowired
    private ISysStaffService sysStaffService;
    @Autowired
    private ISysUserDeptService userDeptService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * index bar 员工列表
     * @return
     */
    public List<RespIndexBarStaff> searchIndexBarStaffList(ReqSearchStaff reqSearchSysStaff) {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        Long deptId = loginUser.getDeptId();

        List<RespIndexBarStaff> indexBarStaffList = Lists.newArrayList();

        Map<String, List<RespStaffInfo>> indexBarMap = Maps.newHashMap();

        // 全部学生列表
        ReqSearchScStudent reqSearchScStudent = new ReqSearchScStudent();
        reqSearchScStudent.setDeptId(deptId);
        reqSearchScStudent.setUserId(loginUser.getUserId());

        List<RespStaffInfo> staffList = sysStaffService.searchStaffList(reqSearchSysStaff, null);
        for (RespStaffInfo staff : staffList) {
            String staffName = staff.getStaffName();
            String pinYin = PinYinUtil.getPinYin(staffName).substring(0,1).toUpperCase();
            if(indexBarMap.containsKey(pinYin)) {
                List<RespStaffInfo> indexListData = indexBarMap.get(pinYin);
                indexListData.add(staff);
            } else {
                List<RespStaffInfo> indexListData = Lists.newArrayList();
                indexListData.add(staff);
                indexBarMap.put(pinYin, indexListData);
            }
        }
        indexBarMap.forEach((key,value) -> {
            indexBarStaffList.add(new RespIndexBarStaff(key,value));
        });
        return indexBarStaffList;
    }

    /**
     * 员工详情
     * @return
     */
    public RespBusinessStaffDetail detailById(Long staffId) {
        RespBusinessStaffDetail respBusinessStaffDetail = new RespBusinessStaffDetail();

        LoginUser loginUser = LoginUserUtil.getLoginUser();

        // 员工信息
        SysStaff staffInfo = sysStaffService.getById(staffId);
        respBusinessStaffDetail.setStaffInfo(staffInfo);

        // 用户名
        if (StringUtils.isNotEmpty(staffInfo.getUserId())) {
            // 员工所属校区
            QueryWrapper<SysUserDept> qwUserDept = new QueryWrapper<>();
            qwUserDept.eq("user_id", staffInfo.getUserId());
            qwUserDept.eq("tenant_id", loginUser.getNowTenantId());
            List<SysUserDept> userDeptList = userDeptService.list(qwUserDept);
            boolean allCampus = userDeptList.stream().anyMatch(item -> item.getDeptId() == -1L);
            if (allCampus) {
                respBusinessStaffDetail.setBelongCampus("all");
                respBusinessStaffDetail.setPartCampusName(org.assertj.core.util.Lists.emptyList());
            } else {
                respBusinessStaffDetail.setBelongCampus("part");

                List<Long> deptIdList = userDeptList.stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
                List<SysDept> deptList = deptService.listByIds(deptIdList);
                List<String> deptNameList = deptList.stream().map(SysDept::getDeptName).collect(Collectors.toList());
                respBusinessStaffDetail.setPartCampusName(deptNameList);
            }
        }

        return respBusinessStaffDetail;
    }

}
